package com.haitao.wavebbs.controller;

import com.haitao.wavebbs.controller.security.checkToken;
import com.haitao.wavebbs.entity.Post;
import com.haitao.wavebbs.service.PostService;
import com.haitao.wavebbs.tools.ControllerTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin(origins = "*")
public class PostController {

    @Autowired
    public PostService postService;

    /**
     * 创建帖子:若成功则返回新建帖子的postId
     *
     * @param post
     * @return postId：新建帖子的id
     */
    @PostMapping("/post")
    @checkToken
    public ResponseEntity createPost(@RequestBody Post post) throws Exception {

//        int userId = (int) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute("userId");
        Integer userId = (Integer) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute("userId");
        Objects.requireNonNull(userId, "用户未授权");
        post.setUserId(userId);
        int postId = postService.createPost(post);
        return ControllerTools.getSuccessResponseEntity(postId, HttpStatus.CREATED);
    }

    /**
     * 返回所有帖子的总数。用于计算分页
     *
     * @return
     */
    @GetMapping("/getPostAmount")
    public ResponseEntity getPostAmount() {
        int postAmount = postService.getPostAmount();
        return ControllerTools.getSuccessResponseEntity(postAmount, HttpStatus.OK);
    }

    /**
     * 查询帖子列表:获取（某个用户）某个分页的贴子
     *
     * @param per_page   每页有几条数据
     * @param pageNumber 请求第几页的数据
     * @param userId     （可选项）查询条件为该userId的用户的帖子
     * @return ArrayList<Post>：该分页的所有帖子
     */
    @GetMapping("/post")
    public ResponseEntity getPosts(@RequestParam int per_page, @RequestParam int pageNumber, @RequestParam(required = false) Integer userId) {
        List<Post> posts = postService.getPosts(per_page, pageNumber, userId);
        return ControllerTools.getSuccessResponseEntity(posts, HttpStatus.OK);
    }

    /**
     * 查询帖子详情：获取某个帖子的详情（不包含评论）
     *
     * @return Post
     */
    @GetMapping("/post/{postId}")
    public ResponseEntity getPostDetails(@RequestParam int postId) {
        Post postByPostId = postService.getPostByPostId(postId);
        return ControllerTools.getSuccessResponseEntity(postByPostId, HttpStatus.OK);
    }

    /**
     * 编辑帖子：编辑某个帖子的content
     *
     * @return null
     */
    @PutMapping("/post/postId")
    public ResponseEntity changePostDetails() {
        return null;
    }

    /**
     * 删除帖子
     *
     * @return null
     */
    @DeleteMapping("/post/postId")
    public ResponseEntity deletePostByPostId() {
        return null;
    }

}
